package com.mjk.common.util;


import com.mjk.common.tools.logger.BizLogger;

import java.text.SimpleDateFormat;
import java.util.*;

public class RandomUtil {
	public static final String ALL_CHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public static final String LETTER_CHAR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public static final String NUMBER_CHAR = "0123456789";
	protected static final String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
			"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
			"t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
			"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
			"W", "X", "Y", "Z" };
	public static final Random random = new Random();
	/**
	 * 获取ComplainNo
     */
	public static String  generateComplainNo(){
		SimpleDateFormat stf=new SimpleDateFormat("yyyyMMddHHmmss");
		int i=10+(random.nextInt()*90);
        return "HTK"+stf.format(new Date())+i;
	}

	/**
	 * 获取八位UUID
	 *
     */
	public static String getShortUuid() {
		StringBuilder shortBuffer = new StringBuilder();
		String uuid = UUID.randomUUID().toString().replace("-", "");
		for (int i = 0; i < 8; i++) {
			String str = uuid.substring(i * 4, i * 4 + 4);
			int x = Integer.parseInt(str, 16);
			shortBuffer.append(chars[x % 0x3E]);
		}
		return shortBuffer.toString();

	}

	/**
	 * 返回一个定长的随机字符串(只包含大小写字母、数字) 　　
	 * 
	 * @param length
	 *            随机字符串长度 　　
	 * @return 随机字符串 　　
	 */
	public static String generateNumber(int length) {
		StringBuilder sb = new StringBuilder();
	
		for (int i = 0; i < length; i++) {
			sb.append(NUMBER_CHAR.charAt(random.nextInt(NUMBER_CHAR.length())));
		}
		return sb.toString();
	}

	/**
	 * 返回一个定长的随机字符串(只包含大小写字母、数字) 　　
	 * 
	 * @param length
	 *            随机字符串长度 　　
	 * @return 随机字符串 　　
	 */
	public static String generateString(int length) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < length; i++) {
			sb.append(ALL_CHAR.charAt(random.nextInt(ALL_CHAR.length())));
		}
		return sb.toString();
	}

	/**
	 * 返回一个定长的随机纯字母字符串(只包含大小写字母) 　　
	 * 
	 * @param length
	 *            随机字符串长度 　　
	 * @return 随机字符串 　　
	 */
	public static String generateMixString(int length) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < length; i++) {
			sb.append(ALL_CHAR.charAt(random.nextInt(LETTER_CHAR.length())));
		}
		return sb.toString();
	}

	/**
	 * 返回一个定长的随机纯大写字母字符串(只包含小写字母) 　
	 * 
	 * @param length
	 *            随机字符串长度 　　
	 * @return 随机字符串 　　
	 */
	public static String generateLowerString(int length) {
		return generateMixString(length).toLowerCase();
	}

	/**
	 * 返回一个定长的随机纯小写字母字符串(只包含大写字母) 　　
	 * 
	 * @param length
	 *            随机字符串长度 　　
	 * @return 随机字符串 　　
	 */
	public static String generateUpperString(int length) {
		return generateMixString(length).toUpperCase();
	}

	/**
	 * 生成一个定长的纯0字符串 　　
	 * 
	 * @param length
	 *            字符串长度
	 * @return 纯0字符串 　　
	 */
	public static String generateZeroString(int length) {
        return "0".repeat(Math.max(0, length));
	}

	/**
	 * 根据数字生成一个定长的字符串，长度不够前面补0 　　
	 * 
	 * @param num
	 *            数字 　　
	 * @param fixdlenth
	 *            字符串长度 　　
	 * @return 定长的字符串 　　
	 */
	public static String toFixdLengthString(long num, int fixdlenth) {
		StringBuilder sb = new StringBuilder();
		String strNum = String.valueOf(num);
		if (fixdlenth - strNum.length() >= 0) {
			sb.append(generateZeroString(fixdlenth - strNum.length()));
		} else {
			BizLogger.info( new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth
					+ "的字符串发生异常！"));
		}

		sb.append(strNum);
		return sb.toString();
	}

	/**
	 * 根据数字生成一个定长的字符串，长度不够前面补0 　　
	 * 
	 * @param num
	 *            数字 　　
	 * @param fixdlenth
	 *            字符串长度 　　
	 * @return 定长的字符串 　　
	 */
	public static String toFixdLengthString(int num, int fixdlenth) {
		StringBuilder sb = new StringBuilder();
		String strNum = String.valueOf(num);
		if (fixdlenth - strNum.length() >= 0) {
			sb.append(generateZeroString(fixdlenth - strNum.length()));
		} else {
			BizLogger.info( new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth
					+ "的字符串发生异常！"));
		}
		sb.append(strNum);
		return sb.toString();
	}

	/**
	 * 生成[min-max]区间的数
     */
	public static int getRadomNum(int min,int max){
        return random.nextInt(max) % (max - min + 1) + min;
	}

}
